package com.shop.service.impl;

import com.shop.dao.CategoryMapper;
import com.shop.dao.ProductAttributeMapper;
import com.shop.dao.ProductMapper;
import com.shop.dao.StoreMapper;
import com.shop.entity.*;
import com.shop.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductAttributeMapper productAttributeMapper;

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private CategoryMapper categoryMapper;


    @Override
    public List findAllProduct(Long storeId) {
        List<Product> productResult = productMapper.findAllProductByStoreId(storeId);

        List<AllProductResult> allProductResult = new ArrayList<>();
        for (int i = 0; i < productResult.size(); i++) {
            List<ProductAttribute> productAttributeResult = productAttributeMapper.findAllProductAttributeByProductId(productResult.get(i).getProductId());
            AllProductResult result = new AllProductResult();
            result.setProductId(productResult.get(i).getProductId());
            result.setProductName(productResult.get(i).getProductName());
            result.setCategoryId(productResult.get(i).getCategoryId());
            result.setStoreId(productResult.get(i).getStoreId());
            result.setAttribute(productAttributeResult);
            allProductResult.add(result);
        }

        return allProductResult;
    }

    @Override
    public int createProduct(Product product) {
        Category category = categoryMapper.selectByCategoryId(product.getCategoryId());
        // 分类不存在或者不是三级分类，则该参数字段异常
        if (category == null || category.getCategoryLevel() != 3) {
            return 50024;
        }
        if (productMapper.selectByCategoryIdAndName(product.getProductName(), product.getCategoryId()) != null) {
            return 50025;
        }
        if (productMapper.insertSelective(product) > 0) {
            return 20000;
        }
        return 50021;
    }

    @Override
    public int createProductAttribute(ProductAttribute productAttribute, Long userId) {
        Product product = productMapper.selectByPrimaryKey(productAttribute.getProductId());
       /* StoreService storeService = new StoreServiceImpl();
        List<Store> allStore = storeService.findAllStore(userId);
        for (Store store:allStore){

        }*/

        if (product == null) {
            return 50026;
        }
        ProductAttribute temp2 = productAttributeMapper.selectByColorAndKit(productAttribute.getColor(), productAttribute.getKit());
        if (temp2 != null && !temp2.getAttributeId().equals(productAttribute.getAttributeId())) {
            //Color和Kit相同且不同AttributeId,不能继续修改
            return 50029;
        }
        if (productAttributeMapper.insertSelective(productAttribute) > 0) {
            return 20000;
        }
        return 50021;
    }

    @Override
    public boolean deleteProduct(Long productId) {
        int res = productAttributeMapper.deleteByProductId(productId);
        int result = productMapper.deleteByPrimaryKey(productId);
        if (result > 0 && res > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean deleteProductAttribute(Long attributeId) {
        int result = productAttributeMapper.deleteByPrimaryKey(attributeId);
        if (result > 0) {
            return true;
        }
        return false;
    }

    @Override
    public int updateProduct(Product product, Long userId) {
        List<Store> allStore = storeMapper.findAllStoreByUserId(userId);
        if (allStore == null) {
            return 50011;
        }
        boolean flag = false;
        for (Store store : allStore) {
            if (store.getStoreId() == product.getStoreId()) {
                flag = true;
            }
        }
        if (!flag) {
            return 50022;
        }


        Category category = categoryMapper.selectByCategoryId(product.getCategoryId());
        // 分类不存在或者不是三级分类，则该参数字段异常
        if (category == null || category.getCategoryLevel() != 3) {
            return 50024;
        }
        Product temp = productMapper.selectByPrimaryKey(product.getProductId());
        if (temp == null) {
            return 50026;
        }
        Product temp2 = productMapper.selectByCategoryIdAndName(product.getProductName(), product.getCategoryId());
        if (temp2 != null && !temp2.getProductId().equals(product.getProductId())) {
            //name和分类id相同且不同id 不能继续修改
            return 50027;
        }
        if (productMapper.updateByPrimaryKeySelective(product) > 0) {
            return 20000;
        }
        return 50022;
    }

    @Override
    public int updateProductAttribute(ProductAttribute productAttribute) {
        ProductAttribute temp = productAttributeMapper.selectByPrimaryKey(productAttribute.getAttributeId());
        if (temp == null) {
            return 50028;
        }
        ProductAttribute temp2 = productAttributeMapper.selectByColorAndKit(productAttribute.getColor(), productAttribute.getKit());
        if (temp2 != null && !temp2.getAttributeId().equals(productAttribute.getAttributeId())) {
            //Color和Kit相同且不同AttributeId,不能继续修改
            return 50029;
        }
        if (productAttributeMapper.updateByPrimaryKeySelective(productAttribute) > 0) {
            return 20000;
        }
        return 50022;
    }

    @Override
    public List searchProduct(String searchInfo) {
        List<Product> productResult = productMapper.findGoodsListBySearch(searchInfo);

//        List<AllProductResult> allProductResult = new ArrayList<>();
//        for (int i = 0; i < productResult.size(); i++) {
//            List<ProductAttribute> productAttributeResult = productAttributeMapper.findAllProductAttributeByProductId(productResult.get(i).getProductId());
//            AllProductResult result = new AllProductResult();
//            result.setProductId(productResult.get(i).getProductId());
//            result.setProductName(productResult.get(i).getProductName());
//            result.setCategoryId(productResult.get(i).getCategoryId());
//            result.setStoreId(productResult.get(i).getStoreId());
//            result.setAttribute(productAttributeResult);
//            allProductResult.add(result);
//        }
        return productResult;
    }

    @Override
    public Map searchProductInfo(Long productId) {
        Product productResult = productMapper.selectByPrimaryKey(productId);
        if (productResult == null) {
            return null;
        }
        List<ProductAttribute> productAttributeResult = productAttributeMapper.findAllProductAttributeByProductId(productResult.getProductId());
        Map map = new HashMap<>();
        map.put("storeId", productResult.getStoreId());
        map.put("storeName", storeMapper.findStoreNameById(productResult.getStoreId()));
        map.put("productName", productResult.getProductName());
        map.put("productId", productResult.getProductId());
        map.put("info", productAttributeResult);
        return map;
    }
}
